package com.jadcon.bedbreakfast.web;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.bedbreakfast.domain.ReservationService;
import com.jadcon.bedbreakfast.domain.Suite;
import com.jadcon.bedbreakfast.domain.SuiteService;

@Controller
@SessionAttributes("suiteSearch")
public class SuiteController {

	@Autowired
	private SuiteService suiteService;
	@Autowired
	private ReservationService reservationService;

	@RequestMapping("/suiteList.html")
	public ModelAndView suite() throws SQLException {

		List<Suite> suiteList = suiteService.getSuiteList();

		ModelAndView mav = new ModelAndView("suiteList");
		mav.addObject("suiteList", suiteList);
		return mav;
	}

	@RequestMapping("/suiteSearch.html")
	public ModelAndView search() {
		return new ModelAndView("suiteSearch");
	}

	@RequestMapping("/suiteSearchResults.html")
	public ModelAndView searchResults(@RequestParam Date checkIn,
			@RequestParam Date checkOut) {

		List<Suite> suiteList = reservationService.equalsDateRange(checkIn,
				checkOut);

		ModelAndView mav = new ModelAndView("suiteSearchResults");
		mav.addObject("suiteList", suiteList);
		return mav;
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}
}
